package com.gzc.web.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.gzc.common.Const;
import com.gzc.service.IUserService;
import com.gzc.util.ActionUtil;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller("loginAction")
@Scope("prototype")
public class LoginAction extends ActionSupport {

	private String username;
	private String password;
	private IUserService userService;
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Autowired
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public String loginInput(){
		return Action.LOGIN;
	}
	
	public String login(){
		ActionContext.getContext().getSession().put(Const.LOGIN_USER_KEY, this.userService.login(username, password));
		ActionUtil.setUrl(Const.USER_LIST_URL);
		return ActionUtil.REDIRECT;
	}
	
	public String logout(){
		ActionContext.getContext().getSession().clear();		
		return Action.LOGIN;
	}

}
